<template>
  <div class="page">
    <el-form label-width="80px" inline :model="searchFrom">
      <el-form-item label="部位名称">
        <el-input v-model="searchFrom.organ"></el-input>
      </el-form-item>
      <el-form-item label="部位症状">
        <el-input v-model="searchFrom.symptom"></el-input>
      </el-form-item>
      <el-form-item label="适用性别">
        <el-select v-model="searchFrom.sex" placeholder="请选择性别" clearable style="width:100%">
          <el-option label="男" value="0" />
          <el-option label="女" value="1" />
          <el-option label="儿童" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="推荐科室">
        <el-select v-model="searchFrom.depart" placeholder="请选择科室" clearable style="width:100%">
          <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="getSmartGuide">搜索</el-button>
        <el-button @click="show()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="organ"
        label="部位名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="symptom"
        label="部位症状"
        width="180">
      </el-table-column>
      <el-table-column
        prop="sex"
        width="200"
        label="适用性别">
        <!-- <template slot-scope="{row}">
          {{ row.sex === 0 ? '男' : row.sex === 1 ? '女' :  row.sex === 2 ? '儿童' : '' }}
        </template> -->
        <template v-if="row.sex" slot-scope="{row}">
          <el-tag v-for="item in row.sex.split(',')" :key="item" size="medium" style="margin-right: 5px;">{{ item == 0 ? '男' : item == 1 ? '女' :  item == 2 ? '儿童' : '' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop=""
        label="推荐科室">
        <template v-if="row.depart" slot-scope="{row}">
          <el-tag v-for="item in row.depart.split(',')" :key="item.id" size="medium" style="margin-right: 5px;">{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="200">
      </el-table-column>
      <el-table-column
        label="操作"
        width="120">
        <template slot-scope="{row}">
          <el-button type="text" @click="show(row)">修改</el-button>
          <el-button type="text" style="color: #F56C6C;" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageNoChange"
      :current-page.sync="page.pageNo"
      :page-size="page.pageSize"
      layout="total, prev, pager, next"
      :total="page.total">
    </el-pagination>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="部位名称">
          <el-select v-model="form.organ" placeholder="请选择部位" style="width:100%">
            <el-option v-for="item in partList" :key="item.name" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="部位症状">
          <el-input v-model="form.symptom" placeholder="请填写症状"></el-input>
        </el-form-item>
        <el-form-item label="适用性别">
          <el-select v-model="form.sex" placeholder="请选择性别" multiple style="width:100%">
            <el-option label="男" value="0" />
            <el-option label="女" value="1" />
            <el-option label="儿童" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="推荐科室">
          <el-select v-model="form.depart" multiple collapse-tags placeholder="请选择科室" style="width:100%">
            <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getSmartGuide, getPart, smartGuideAdd, smartGuideUpdate, smartGuideDelete, getDepart } from '@/api/official/official'
export default {
  name: 'InternetHospitalMangeSmartGuide',

  data() {
    return {
      loading: false,
      searchFrom: {},
      page:{
        total:0,
        pageSize:10,
        pageNo:1,
      },
      tableData: [],
      partList: [],
      deptList: [],
      dialogVisible: false,
      form: {}
    };
  },

  mounted() {
    this.getSmartGuide()
    this.getPart()
    this.getDepart()
  },

  methods: {
    parseDepart(depart) {
      try {
        return JSON.parse(depart);
      } catch (error) {
        console.error('解析 JSON 失败:', error);
        return []; // 返回空数组或其他默认值
      }
    },
    getSmartGuide() {
      const data = {
        current: this.page.pageNo,
        size: this.page.pageSize,
        ...this.searchFrom
      }
      getSmartGuide(data).then(res => {
        this.tableData = res.records
        this.page.total = res.total
      })
    },
    pageSizeChange(val){
      this.page.pageSize = val;
      this.getSmartGuide();
    },
    pageNoChange(val){
      this.page.pageNo = val;
      this.getSmartGuide();
    },
    getPart() {
      getPart({size: 999}).then(res => {
        this.partList = res.records
      })
    },
    getDepart() {
      function unique(arr) {
        const res = new Map();
        return arr.filter((item) => !res.has(item.name) && res.set(item.name, 1));
      }
      getDepart({size: 999}).then(res => {
        this.deptList = unique(res.content)
      })
    },
    show(row = {}) {
      this.dialogVisible = true
      this.form = {...row, depart: row.depart.split(','), sex: row.sex.split(',') }
    },
    del(id) {
      smartGuideDelete({ids: [id]}).then(res => {
        this.$message.success('删除成功')
        this.getSmartGuide()
      })
    },
    submit() {
      this.loading = true
      const data = {
        ...this.form,
        depart: this.form.depart.join(),
        sex: this.form.sex.join()
      }
      if (this.form.id) {
        smartGuideUpdate(data).then(res => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getSmartGuide()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      } else {
        smartGuideAdd(data).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.getSmartGuide()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.page {
  padding: 24px;
}
</style>
